//
//  MKWPool.m
//  MilkyWayReader
//
//  Created by Quoc Dung Chu on 31/03/12.
//  Copyright (c) 2012 Milky Way Mobile. All rights reserved.
//

#import "MKWPool.h"
@interface MKWPool () 
@property (nonatomic, strong) NSMutableArray * mainQueue;
@end

@implementation MKWPool
@synthesize mainQueue;

- (NSMutableArray *) mainQueue
{
    if (mainQueue == nil)
    {
        mainQueue = [NSMutableArray array];
    }
    return mainQueue;
}



- (void) performInAsyncTaskDoBeginAction: (DoBeginAction) doBeginAction_ 
                    doInBackgroundAction: (DoInBackgroundAction) doInBackgroundAction_
                        doProgressAction: (DoProgressAction) doProgressAction_ 
                          doFinishAction: (DoFinishAction) doFinishAction_ 
{
    MKWAsyncTask * asyncTask = [MKWAsyncTask createWithDoBeginAction:doBeginAction_ doInBackgroundAction:doInBackgroundAction_ doProgressAction:doProgressAction_ doFinishAction:doFinishAction_];
    asyncTask.delegate = self;
    [self.mainQueue addObject:asyncTask];
    if (self.mainQueue.count == 1) {
        [asyncTask execute];
    }
    
}

- (void) didMKWAsyncTaskFinshExecute: (MKWAsyncTask *) asyncTask_
{
    [self.mainQueue removeObject:asyncTask_];
    if (self.mainQueue.count >= 1) {
        MKWAsyncTask * nextTask = [self.mainQueue objectAtIndex:0];
        [nextTask execute];
    }
}

@end
